#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define NEW_LED_DRV     1
#define NEW_LED_NAME    "dtsled"

#define LEDON   1
#define LEDOFF  0

static void __iomem *PA_CFG1;
static void __iomem *PA_DATA;

/* dtsled设备结构体 */
struct dtsled_dev{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;		/* 类 		*/
	struct device *device;	/* 设备 	 */
	int major;				/* 主设备号	  */
	int minor;				/* 次设备号   */
	struct device_node	*nd; /* 设备节点 */
};

struct dtsled_dev dtsled;	/* led设备 */

//香橙派灯是高电平亮，低电平灭
void led_switch(u8 sta)
{
    u32 val = 0;
	if(sta == LEDON) {
		val = readl(PA_DATA);
		//val &= ~(1 << 15);
		val |= (1 << 15);//置1	
		writel(val, PA_DATA);
	}else if(sta == LEDOFF) {
		val = readl(PA_DATA);
		//val |= (1 << 15);
		val &= ~(1 << 15);//置0	
		writel(val, PA_DATA);
	}	
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件，file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int led_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &dtsled; /* 设置私有数据 */
	return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
	int retvalue;
	unsigned char databuf[1];
	unsigned char ledstat;

	retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) {
		printk("kernel write failed!\r\n");
		return -EFAULT;
	}

	ledstat = databuf[0];		/* 获取状态值 */

	if(ledstat == LEDON) {	
		led_switch(LEDON);		/* 打开LED灯 */
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
	}
	return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static struct file_operations dtsled_fops = {
	.owner = THIS_MODULE,
	.open = led_open,
	//.read = led_read,
	.write = led_write,
	.release = 	led_release,
};

static int __init led_init(void)
{
	u32 val = 0;
	int ret;
	u32 regdata[14];
	const char *str;
	struct property *proper;

	/* 获取设备树中的属性数据 */
	/* 1、获取设备节点：alphaled */
	dtsled.nd = of_find_node_by_path("/alphaled");
	if(dtsled.nd == NULL) {
		printk("alphaled node nost find!\r\n");
		return -EINVAL;
	} else {
		printk("alphaled node find!\r\n");
	}

	/* 2、获取compatible属性内容 */
	proper = of_find_property(dtsled.nd, "compatible", NULL);
	if(proper == NULL) {
		printk("compatible property find failed\r\n");
	} else {
		printk("compatible = %s\r\n", (char*)proper->value);
	}

	/* 3、获取status属性内容 */
	ret = of_property_read_string(dtsled.nd, "status", &str);
	if(ret < 0){
		printk("status read failed!\r\n");
	} else {
		printk("status = %s\r\n",str);
	}

	/* 4、获取reg属性内容 */
	ret = of_property_read_u32_array(dtsled.nd, "reg", regdata, 10);
	if(ret < 0) {
		printk("reg property read failed!\r\n");
	} else {
		u8 i = 0;
		printk("reg data:\r\n");
		for(i = 0; i < 10; i++)
			printk("%#X ", regdata[i]);
		printk("\r\n");
	}

    PA_CFG1 = of_iomap(dtsled.nd , 0);
    PA_DATA = of_iomap(dtsled.nd , 1);

    val=readl(PA_CFG1);
    val&=~(1<<28);
    val|=(1<<28);
    writel(val,PA_CFG1);

    /*注册字符设备驱动*/
    if(dtsled.major){
        dtsled.devid = MKDEV(dtsled.major,0);
        register_chrdev_region(dtsled.devid, NEW_LED_DRV , NEW_LED_NAME);
    }else{
        alloc_chrdev_region(&dtsled.devid, 0,NEW_LED_DRV , NEW_LED_NAME);
        dtsled.major = MAJOR(dtsled.devid);
        dtsled.minor = MINOR(dtsled.devid);
    }
    printk("newchrled major=%d,minor=%d \r\n",dtsled.major,dtsled.minor);

    dtsled.cdev.owner = THIS_MODULE;
    cdev_init(&dtsled.cdev , &dtsled_fops);

    cdev_add(&dtsled.cdev, dtsled.devid, NEW_LED_DRV);

    dtsled.class = class_create(THIS_MODULE, NEW_LED_NAME);
    if (IS_ERR(dtsled.class)){
        return PTR_ERR(dtsled.class);
    }

    dtsled.device = device_create(dtsled.class, NULL, dtsled.devid, NULL, NEW_LED_NAME);
    if (IS_ERR(dtsled.device)) {
		return PTR_ERR(dtsled.device);
	}

    return 0;
}

static void __exit led_exit(void)
{
    iounmap(PA_CFG1);
    iounmap(PA_DATA);

    //unregister_chrdev(LED_MAJOR, LED_NAME);
    cdev_del(&dtsled.cdev);
    unregister_chrdev_region(dtsled.devid, NEW_LED_DRV);

    device_destroy(dtsled.class, dtsled.devid);
    class_destroy(dtsled.class);
}

module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("HRX");
